package com.xu.ai.chatclient.controller;

import java.util.List;

import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingOptions;

import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.EmbeddingRequest;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * EmbeddingModel controller 嵌入的工作原理是将文本、图像和视频转换为浮点数数组，称为向量。
 * 这些向量旨在捕获文本、图像和视频的含义。嵌入数组的长度称为向量的维度。
 *
 * @author xuguan
 * @since 2025/10/18
 */
@RestController
@RequestMapping("/api/embedding")
public class EmbeddingController {

	private final EmbeddingModel embeddingModel;

	public EmbeddingController(EmbeddingModel embeddingModel) {
		this.embeddingModel = embeddingModel;
	}

	@GetMapping(path = "/call")
	public EmbeddingResponse call() {
		final EmbeddingRequest request = new EmbeddingRequest(List.of("中国的首都是北京", "美国的首都是华盛顿"),
				DashScopeEmbeddingOptions.builder().withModel("text-embedding-v4").build());
		final EmbeddingResponse response = embeddingModel.call(request);
		final float[] embedding = response.getResult().getOutput();
		return response;
	}

}
